$(document).ready(function () {
	$("#login").addClass("active");
	//用户电话为空和ajax
	$("#user_phone").blur(function(){
		if ($("#user_phone").val() === "") {
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码不能为空");
		}else if (!checkPhone($("#user_phone").val())){
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码格式不正确");
		} else {
			$.ajax({
				type:'post',
				url:'/login/checkUser.action',
				dataType:'json',
				data:{
				    user_phone:$("#user_phone").val(),
					user_type :$("#user_type option:selected").val()
				},
				success:function(data){
					if (data.status === "User_Not_Registered") {
						swal({
								title:data.status,
								text:"尚未注册",
								type:"warning",
								showCancelButton:true,
								confirmButtonColor:"#DD6B55",
								confirmButtonText:"立即注册",
								closeOnConfirm:false
							},
							function () {
								//跳到注册页面http://www.dglives.com/demo/sweetalert-master/example/
								window.location.href = "/register/jumpToRegisterPage.action?flag="+$("#user_type option:selected").val();
							});
					} else if (data.status === "User_Empty") {
						$("#user_phone").addClass("form-error");
						$("#phone_result").html(data.msg);
					}else if (data.status === "User_Exist"){
						$("#user_phone").removeClass("form-error");
						$("#phone_result").html("");
					}else if (data.status === "Check_Fail"){
						swal({
								title:data.status,
								text:"服务异常",
								type:"warning",
								showCancelButton:true,
								confirmButtonColor:"#DD6B55",
								confirmButtonText:"重新登录",
								closeOnConfirm:false
							},
							function () {
								//跳到注册页面http://www.dglives.com/demo/sweetalert-master/example/
								window.location.reload();
							});
					}else if (data.status === "Incorrect_Phone"){
						sweetAlert("WARNING","电话号码格式不正确","warning");
					} else {
						swal({
								title:data.status,
								text:"网络异常",
								type:"warning",
								showCancelButton:true,
								confirmButtonColor:"#DD6B55",
								confirmButtonText:"重新登录",
								closeOnConfirm:false
							},
							function () {
								//跳到注册页面http://www.dglives.com/demo/sweetalert-master/example/
								window.location.reload();
							});
					}
				},
				error:function(jqXHR){
					//alert("发生错误" + jqXHR.status);
					sweetAlert("发生错误",jqXHR.status,"error");
				}
			});
		}
		submitCondition();
	});
	//密码为空
	$("#user_pwd").blur(function(){
		if ($("#user_pwd").val() === "") {
			$("#user_pwd").addClass("form-error");
			$("#pwd_result").html("密码不能为空");
		} else {
			$("#user_pwd").removeClass("form-error");
			$("#pwd_result").html("");
		}
		submitCondition();
	});
	//下面是按下登录按钮之后的操作
	$("#submit").on('click', function(){
		if ($("#user_phone").val() === "") {
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码不能为空");
		}else if (!checkPhone($("#user_phone").val())){
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码格式不正确");
		}else if ($("#user_pwd").val() === ""){
			$("#user_pwd").addClass("form-error");
			$("#pwd_result").html("密码不能为空");
		}else {
			$.ajax({
				type: 'post',
				//下面url地址由后台补上
				url: '/login/login.action',
				dataType: 'json',
				data: {
					user_phone: $("#user_phone").val(),
					user_password: $("#user_pwd").val(),
					user_type: $("#user_type option:selected").val()
				},
				timeout:60000,
				beforeSend:function () {
					$("#submit").empty();
					$("#submit").html("<img height='30' width='30' src='/image/loading.gif'/>");
					$("#submit").addClass("disabled");
				},
				success: function (data) {
					if (data.status === "User_Empty") {
						sweetAlert("WARNING","请填写完整的用户信息","warning");
					}else if (data.status === "Login_Repeated") {
						swal({
						 title: data.status,
						 text: "您已经登录，请勿重复登录",
						 type: "warning",
						 showCancelButton: false,
						 confirmButtonColor: "#DD6B55",
						 confirmButtonText: "确定",
						 closeOnConfirm: false
						 },
						 function () {
							 //跳到注册页面http://www.dglives.com/demo/sweetalert-master/example/
							 window.location.href = "/index/index.action";
						 });
						setTimeout(function(){
							window.location.href = "/index/index.action";
						},3000);
					} else if (data.status === "Incorrect_Phone_Format") {
						$("#user_phone").addClass("form-error");
						$("#phone_result").html(data.msg);
					} else if (data.status === "Unregistered_User") {
						swal({
								title: data.status,
								text: "尚未注册",
								type: "warning",
								showCancelButton: true,
								confirmButtonColor: "#DD6B55",
								confirmButtonText: "立即注册",
								closeOnConfirm: false
							},
							function () {
								//跳到注册页面http://www.dglives.com/demo/sweetalert-master/example/
								window.location.href = "/register/jumpToRegisterPage.action?flag=" + $("#user_type option:selected").val();
							});
					} else if (data.status === "Incorrect_Password") {
						$("#user_pwd").addClass("form-error");
						$("#pwd_result").html(data.status);
					} else if (data.status === "Login_Success") {
						//登录成功
						swal({
								title: "登录成功",
								text: "正在跳转",
								type: "success",
								showCancelButton: false,
								confirmButtonColor: "#DD6B55",
								confirmButtonText: "确定",
								closeOnConfirm: false
							},
							function () {
								//跳到注册页面http://www.dglives.com/demo/sweetalert-master/example/
								window.location.href = "/index/index.action";
							});
						setTimeout(function(){
							window.location.href = "/index/index.action";
						},3000);
					}else if (data.status === "Get_userInfo_Fail"){
						console.error(data.msg);
					}else if (data.status === "MessageActivity_Empty"){
						console.error(data.msg);
					}else if (data.status === "Get_MessageActivity_Fail"){
						console.error(data.msg);
					}else if (data.status === "Get_MessageNum_Fail"){
						console.error(data.msg);
					}else if (data.status === "Get_SearchHistory_Fail"){
						console.error(data.msg);
					}else if (data.status === "Get_MostLikeSearch_Fail"){
						console.error(data.msg);
					}else if (data.status === "Get_OwnEntity_Fail"){
						console.error(data.msg);
					}else if (data.status === "History_Empty"){
						console.error(data.msg);
					}else if (data.status === "Page_Empty"){
						console.error(data.msg);
					}else if (data.status === "Get_OwnEntityList_Fail"){
						console.error(data.msg);
					}else if (data.status === "Get_OwnNumbers_Fail"){
						console.error(data.msg);
					}else {
						swal({
								title: data.status,
								text: "登录异常",
								type: "warning",
								showCancelButton: true,
								confirmButtonColor: "#DD6B55",
								confirmButtonText: "重新登录",
								closeOnConfirm: false
							},
							function () {
								//跳到登录页面
								window.location.href = "/login/jumpTologinPage.action";
							});
					}
				},
				complete:function () {
					$("#submit").empty();
					$("#submit").html("登录");
					$("#submit").removeClass("disabled");
				},
				error: function (jqXHR) {
					$("#submit").empty();
					$("#submit").html("登录");
					$("#submit").removeClass("disabled");
					sweetAlert("发生错误",jqXHR.status,"error");
				}
			});
		}
		submitCondition();
	});
});
//表单存在没填选项，提交按钮为disabled
function submitCondition(){
	if ( $("*").hasClass("form-error")) {
		$("#submit").addClass("disabled");
	} else {
		$("#submit").removeClass("disabled");
	}
}

function checkPhone(phone) {
	var rex = /^1[3|4|5|8][0-9]\d{4,8}$/
	if (rex.test(phone)){
		return true;
	}else {
		return false;
	}
}